import Vue from 'vue'
import App from './App.vue'
import store from './store'

import 'ant-design-vue/dist/antd.css';
import {Modal,message} from 'ant-design-vue'
Vue.prototype.$amodal=Modal;
Vue.prototype.$amessage=message;
Vue.use(Modal)


import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)
import '../static/utils/jquery.js'
import VueLazyload from 'vue-lazyload'
Vue.use(VueLazyload)

import VueClipboard from 'vue-clipboard2';
VueClipboard.config.autoSetContainer = true;
Vue.use(VueClipboard)

import SparkMD5 from 'spark-md5'

import {VueMasonryPlugin} from 'vue-masonry';
Vue.use(VueMasonryPlugin)

import bhlper from  '../static/utils/browserHelper.js'
Vue.prototype.$bhelper=bhlper;

//vue中引用
import 'nprogress/nprogress.css'

import './assets/style/style.css'

import './assets/style/theme.less'

let imgUrlExist = function(url) {
  return new Promise((resolve) => {
      let img = new Image();
      img.src = url;
      img.onload = function() {
          resolve(true);
          img = null;
      }
      img.onerror = function() {
          resolve(false);
          img = null;
      }
  })
}

let defaultLoadingImg=require('@/assets/no_pic.png');
let domainUrl='https://img.gameui.net/';//图片服务器域名
Vue.prototype.$imgdomainurl=domainUrl;
Vue.prototype.$webdomainurl='https://www.gameui.net';//表情域名前缀
Vue.prototype.$savePublishAlihost='http://image.gameuiux.cn';//图片附件上传域名前缀

// 作品图片、原图
Vue.directive('imgd',{
  inserted:async function(el,bind){
    let _ditem=bind.value;
    let _vtime=new Date(_ditem.uptime).getTime();
    let preStr='article-';
    if(_ditem.isimg){
      preStr='';
    }
    let _src=domainUrl+preStr+_ditem.id+'-'+_vtime+'@1x'+_ditem.w+'.webp';

    let exist = await imgUrlExist(_src);
    if(exist){
      el.src=_src;
      // el.src={domainUrl}`article-${_ditem.id}-${_vtime}@1x${_ditem.w}.webp`;
      let srcsets=[];
      srcsets.push(domainUrl+preStr+_ditem.id+'-'+_vtime+'@1x'+_ditem.w+'.webp 1x');
      srcsets.push(domainUrl+preStr+_ditem.id+'-'+_vtime+'@2x'+_ditem.w+'.webp 2x');
      srcsets.push(domainUrl+preStr+_ditem.id+'-'+_vtime+'@3x'+_ditem.w+'.webp 3x');
      el.srcset=srcsets.join(',')
    }else{
      el.src=defaultLoadingImg;
      el.removeAttribute('srcset')
    }
  },
  update:async function(el,bind){
    let _ditem=bind.value;
    let _vtime=new Date(_ditem.uptime).getTime();
    let preStr='article-';
    if(_ditem.isimg){
      preStr='';
    }
    let _src=domainUrl+preStr+_ditem.id+'-'+_vtime+'@1x'+_ditem.w+'.webp';

    let exist = await imgUrlExist(_src);
    if(exist){
      el.src=_src;
      // el.src={domainUrl}`article-${_ditem.id}-${_vtime}@1x${_ditem.w}.webp`;
      let srcsets=[];
      srcsets.push(domainUrl+preStr+_ditem.id+'-'+_vtime+'@1x'+_ditem.w+'.webp 1x');
      srcsets.push(domainUrl+preStr+_ditem.id+'-'+_vtime+'@2x'+_ditem.w+'.webp 2x');
      srcsets.push(domainUrl+preStr+_ditem.id+'-'+_vtime+'@3x'+_ditem.w+'.webp 3x');
      el.srcset=srcsets.join(',')
    }else{
      el.src=defaultLoadingImg;
      el.removeAttribute('srcset')
    }
  }
})

let defaultAvatar=require('@/assets/pro/default_uimg.svg');

// 用户图像
Vue.directive('aimgd',{
  inserted:async function(el,bind){
    let _ditem=bind.value;
    el.title=_ditem.name;
    if (_ditem.avatar) {
      let exist = await imgUrlExist(_ditem.avatar);
      if (exist) {
        el.src=_ditem.avatar;
        // let hash = SparkMD5.hash(_ditem.avatar).slice(0, 6)
        // el.src=`${domainUrl}/avatar-${_ditem.id}-${hash}@1x${_ditem.w}.webp`;
        // el.srcset=`${domainUrl}/avatar-${_ditem.id}-${hash}@1x${_ditem.w}.webp 1x,${domainUrl}/avatar-${_ditem.id}-${hash}@2x${_ditem.w}.webp 2x,${domainUrl}/avatar-${_ditem.id}-${hash}@3x${_ditem.w}.webp 3x`;
      } else {
        el.src=defaultAvatar;
        // el.srcset=defaultAvatar;
      }
    }else{
      el.src=defaultAvatar;
      // el.srcset=defaultAvatar;
    }
  },
  update:async function(el,bind){
    let _ditem=bind.value;
    let defaultAvatar=require('@/assets/pro/default_uimg.svg');
    el.title=_ditem.name;
    if (_ditem.avatar) {
      let exist = await imgUrlExist(_ditem.avatar);
      if (exist) {
        el.src=_ditem.avatar;
        // let hash = SparkMD5.hash(_ditem.avatar).slice(0, 6)
        // el.src=`${domainUrl}/avatar-${_ditem.id}-${hash}@1x${_ditem.w}.webp`;
        // el.srcset=`${domainUrl}/avatar-${_ditem.id}-${hash}@1x${_ditem.w}.webp 1x,${domainUrl}/avatar-${_ditem.id}-${hash}@2x${_ditem.w}.webp 2x,${domainUrl}/avatar-${_ditem.id}-${hash}@3x${_ditem.w}.webp 3x`;
      } else {
        el.src=defaultAvatar;
        // el.srcset=defaultAvatar;
      }
    }else{
      el.src=defaultAvatar;
      // el.srcset=defaultAvatar;
    }
  }
})

Vue.config.productionTip = false
// 返回顶部

router.beforeEach((to, from, next) => {
  $('body,html').animate({
    'scrollTop':0
  }, 0);
  if(to.meta&&to.meta.title){
    document.title=to.meta.title;
  }
  next()
})

new Vue({
  store,
  router,
  render: h => h(App)
}).$mount('#app')
